package com.aaa.power.service.impl;



import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.dao.DictDataDao;
import com.aaa.power.entity.DictData;
import com.aaa.power.service.DictDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 字典数据表(DictData)表服务实现类
 *
 * @author makejava
 * @since 2020-11-17 20:15:06
 */
@Service("dictDataService")
public class DictDataServiceImpl implements DictDataService {
    @Resource
    private DictDataDao dictDataDao;

    /**
     * 通过ID查询单条数据
     *
     * @param dictCode 主键
     * @return 实例对象
     */
    @Override
    public Result queryById(Integer dictCode) {
        if (dictCode == null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(), ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result<>(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg(), this.dictDataDao.queryById(dictCode));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public Result queryAllByLimit(int offset, int limit) {
        if (offset == 0 || limit == 0) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(), ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return new Result<>(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg(), this.dictDataDao.queryAllByLimit(offset, limit));
    }

    /**
     * 新增数据
     *
     * @param dictData 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(DictData dictData) {
        if (dictData == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(), ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        this.dictDataDao.insert(dictData);
        return new Result<>(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg(), dictData);
    }

    /**
     * 修改数据
     *
     * @param dictData 实例对象
     * @return 实例对象
     */
    @Override
    public Result update(DictData dictData) {
        this.dictDataDao.update(dictData);
        return this.queryById(dictData.getDictCode());
    }

    /**
     * 通过主键删除数据
     *
     * @param dictCode 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(Integer dictCode) {
        if (dictCode == null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(), ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result<>(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg(), this.dictDataDao.deleteById(dictCode) > 0);
    }

    @Override
    public Result queryAllDictType(Integer pageNo, Integer pageSize, DictData dictData) {
        if (pageNo == 0 || pageSize == 0) {
            throw  new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<DictData> pageInfo = new PageInfo<DictData>(dictDataDao.queryAllDictType(dictData));
        return new Result(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg(), pageInfo);
    }
}